home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxpath.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  14KB  |  439 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxpath.c */
  20. /* Internal path construction routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxfixed.h"
  24. #include "gzpath.h"
  25.  
  26. /* These routines all assume that all points are */
  27. /* already in device coordinates, and in fixed representation. */
  28. /* As usual, they return either 0 or a (negative) error code. */
  29.  
  30. /* Forward references */
  31. private subpath *path_alloc_copy(P1(gx_path *));
  32. private int gx_path_new_subpath(P1(gx_path *));
  33. #ifdef DEBUG
  34. void gx_print_segment(P1(const segment *));
  35. #endif
  36.  
  37. /* Macro for checking a point against a preset bounding box. */
  38. #define check_in_bbox(ppath, px, py)\
  39.   if ( px < ppath->bbox.p.x || px > ppath->bbox.q.x ||\
  40.        py < ppath->bbox.p.y || py > ppath->bbox.q.y\
  41.      )\
  42.     return gs_error_rangecheck
  43.  
  44. /* ------ Initialize/free paths ------ */
  45.  
  46. /* Initialize a path */
  47. void
  48. gx_path_init(gx_path *ppath, const gs_memory_procs *pprocs)
  49. {    ppath->memory_procs = pprocs;
  50.     gx_path_reset(ppath);
  51. }
  52. void
  53. gx_path_reset(register gx_path *ppath)
  54. {    ppath->box_last = 0;
  55.     ppath->position_valid = 0;
  56.     ppath->first_subpath = ppath->current_subpath = 0;
  57.     ppath->subpath_count = 0;
  58.     ppath->curve_count = 0;
  59.     ppath->subpath_open = 0;
  60.     ppath->shares_segments = 0;
  61.     ppath->bbox_set = 0;
  62. }
  63.  
  64. /* Release the contents of a path.  We do this in reverse order */
  65. /* so as to maximize LIFO allocator behavior. */
  66. void
  67. gx_path_release(gx_path *ppath)
  68. {    segment *pseg;
  69.     if ( ppath->first_subpath == 0 ) return;    /* empty path */
  70.     if ( ppath->shares_segments ) return;    /* segments are shared */
  71.     pseg = (segment *)ppath->current_subpath->last;
  72.     while ( pseg )
  73.        {    segment *prev = pseg->prev;
  74.         static uint sizes[] = { segment_type_sizes };
  75. #ifdef DEBUG
  76. if ( gs_debug['p'] )
  77.         dprintf("[p]release"), gx_print_segment(pseg);
  78. #endif
  79.         (*ppath->memory_procs->free)((char *)pseg, 1, sizes[(int)pseg->type], "gx_path_release");
  80.         pseg = prev;
  81.        }
  82.     ppath->first_subpath = 0;    /* prevent re-release */
  83. }
  84.  
  85. /* Mark a path as shared */
  86. void
  87. gx_path_share(gx_path *ppath)
  88. {    if ( ppath->first_subpath ) ppath->shares_segments = 1;
  89. }
  90.  
  91. /* ------ Incremental path building ------ */
  92.  
  93. /* Macro for opening the current subpath. */
  94. /* ppath points to the path; psub has been set to ppath->current_subpath. */
  95. #define path_open()\
  96.     if ( !ppath->subpath_open )\
  97.        {    int code;\
  98.         if ( !ppath->position_valid )\
  99.           return_error(gs_error_nocurrentpoint);\
  100.         code = gx_path_new_subpath(ppath);\
  101.         if ( code < 0 ) return code;\
  102.         psub = ppath->current_subpath;\
  103.        }
  104.  
  105. /* Macros for allocating path segments. */
  106. /* Note that they assume that ppath points to the path, */
  107. /* and that psub points to the current subpath. */
  108. /* We have to split the macro into two because of limitations */
  109. /* on the size of a single statement (sigh). */
  110. #define p_alloc(pseg,size)\
  111.   if_debug2('A', "[p]%lx<%u>\n", (ulong)pseg, size)
  112. #define path_unshare(ppath)\
  113.   if(ppath->shares_segments)\
  114.     if(!(psub = path_alloc_copy(ppath)))return_error(gs_error_limitcheck)
  115. #define path_alloc_segment(pseg,ctype,stype,cname)\
  116.   path_unshare(ppath);\
  117.   if( !(pseg = (ctype *)(*ppath->memory_procs->alloc)(1, sizeof(ctype), cname)) )\
  118.     return_error(gs_error_limitcheck);\
  119.   p_alloc((char *)pseg, sizeof(ctype));\
  120.   pseg->type = stype, pseg->next = 0
  121. #define path_alloc_link(pseg)\
  122.   { segment *prev = psub->last;\
  123.     prev->next = (segment *)pseg;\
  124.     pseg->prev = prev;\
  125.     psub->last = (segment *)pseg;\
  126.   }
  127.  
  128. /* Open a new subpath */
  129. private int
  130. gx_path_new_subpath(gx_path *ppath)
  131. {    subpath *psub = ppath->current_subpath;
  132.     register subpath *spp;
  133.     path_alloc_segment(spp, subpath, s_start, "gx_path_new_subpath");
  134.     spp->last = (segment *)spp;
  135.     spp->curve_count = 0;
  136.     spp->closed = 0;
  137.     spp->pt = ppath->position;
  138.     ppath->subpath_open = 1;
  139.     if ( !psub )            /* first subpath */
  140.        {    ppath->first_subpath = spp;
  141.         spp->prev = 0;
  142.        }
  143.     else
  144.        {    segment *prev = psub->last;
  145.         prev->next = (segment *)spp;
  146.         spp->prev = prev;
  147.        }
  148.     ppath->current_subpath = spp;
  149.     ppath->subpath_count++;
  150. #ifdef DEBUG
  151. if ( gs_debug['p'] )
  152.     dprintf("[p]"), gx_print_segment((const segment *)spp);
  153. #endif
  154.     return 0;
  155. }
  156.  
  157. /* Add a point to the current path (moveto). */
  158. int
  159. gx_path_add_point(register gx_path *ppath, fixed x, fixed y)
  160. {    if ( ppath->bbox_set )
  161.         check_in_bbox(ppath, x, y);
  162.     ppath->subpath_open = 0;
  163.     ppath->position_valid = 1;
  164.     ppath->position.x = x;
  165.     ppath->position.y = y;
  166.     return 0;
  167. }
  168.  
  169. /* Add a relative point to the current path (rmoveto). */
  170. int
  171. gx_path_add_relative_point(register gx_path *ppath, fixed dx, fixed dy)
  172. {    if ( !ppath->position_valid )
  173.       return_error(gs_error_nocurrentpoint);
  174.     if ( ppath->bbox_set )
  175.       check_in_bbox(ppath, ppath->position.x + dx, ppath->position.y + dy);
  176.     ppath->subpath_open = 0;
  177.     ppath->position.x += dx;
  178.     ppath->position.y += dy;
  179.     return 0;
  180. }
  181.  
  182. /* Set the segment point and the current point in the path. */
  183. /* Assumes ppath points to the path. */
  184. #define path_set_point(pseg, fx, fy)\
  185.     (pseg)->pt.x = ppath->position.x = (fx),\
  186.     (pseg)->pt.y = ppath->position.y = (fy)
  187.  
  188. /* Add a line to the current path (lineto). */
  189. int
  190. gx_path_add_line(gx_path *ppath, fixed x, fixed y)
  191. {    subpath *psub = ppath->current_subpath;
  192.     register line_segment *lp;
  193.     if ( ppath->bbox_set )
  194.         check_in_bbox(ppath, x, y);
  195.     path_open();
  196.     path_alloc_segment(lp, line_segment, s_line, "gx_path_add_line");
  197.     path_alloc_link(lp);
  198.     path_set_point(lp, x, y);
  199. #ifdef DEBUG
  200. if ( gs_debug['p'] )
  201.     dprintf("[p]"), gx_print_segment((segment *)lp);
  202. #endif
  203.     return 0;
  204. }
  205.  
  206. /* Add a rectangle to the current path. */
  207. /* This is a special case of adding a parallelogram. */
  208. int
  209. gx_path_add_rectangle(gx_path *ppath, fixed x0, fixed y0, fixed x1, fixed y1)
  210. {    return gx_path_add_pgram(ppath, x0, y0, x0, y1, x1, y1);
  211. }
  212.  
  213. /* Add a parallelogram to the current path. */
  214. /* This is equivalent to an add_point, three add_lines, */
  215. /* and a close_subpath. */
  216. int
  217. gx_path_add_pgram(gx_path *ppath,
  218.   fixed x0, fixed y0, fixed x1, fixed y1, fixed x2, fixed y2)
  219. {    int code;
  220.      if (    (code = gx_path_add_point(ppath, x0, y0)) < 0 ||
  221.         (code = gx_path_add_line(ppath, x1, y1)) < 0 ||
  222.         (code = gx_path_add_line(ppath, x2, y2)) < 0 ||
  223.         (code = gx_path_add_line(ppath, x0 + x2 - x1, y0 + y2 - y1)) < 0 ||
  224.         (code = gx_path_close_subpath(ppath)) < 0
  225.        ) return code;
  226.     return 0;
  227. }
  228.  
  229. /* Add a curve to the current path (curveto). */
  230. int
  231. gx_path_add_curve(gx_path *ppath,
  232.   fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3)
  233. {    subpath *psub = ppath->current_subpath;
  234.     register curve_segment *lp;
  235.     if ( ppath->bbox_set )
  236.     {    check_in_bbox(ppath, x1, y1);
  237.         check_in_bbox(ppath, x2, y2);
  238.         check_in_bbox(ppath, x3, y3);
  239.     }
  240.     path_open();
  241.     path_alloc_segment(lp, curve_segment, s_curve, "gx_path_add_curve");
  242.     path_alloc_link(lp);
  243.     lp->p1.x = x1;
  244.     lp->p1.y = y1;
  245.     lp->p2.x = x2;
  246.     lp->p2.y = y2;
  247.     path_set_point(lp, x3, y3);
  248.     psub->curve_count++;
  249.     ppath->curve_count++;
  250. #ifdef DEBUG
  251. if ( gs_debug['p'] )
  252.     dprintf("[p]"), gx_print_segment((segment *)lp);
  253. #endif
  254.     return 0;
  255. }
  256.  
  257. /*
  258.  * Add an approximation of an arc to the current path.
  259.  * Parameters are the initial and final points of the arc,
  260.  * and the point at which the extended tangents meet.
  261.  * We assume that the arc is less than a semicircle.
  262.  * The arc may go either clockwise or counterclockwise.
  263.  * The approximation is a very simple one: a single curve
  264.  * whose other two control points are a fraction F of the way
  265.  * to the intersection of the tangents, where
  266.  *    F = (4/3)(1 / (1 + sqrt(1+(d/r)^2)))
  267.  * where r is the radius and d is the distance from either tangent
  268.  * point to the intersection of the tangents.  This produces
  269.  * a curve whose center point, as well as its ends, lies on
  270.  * the desired arc.
  271.  *
  272.  * Because F has to be computed in user space, we let the client
  273.  * compute it and pass it in as an argument.
  274.  */
  275. int
  276. gx_path_add_arc(gx_path *ppath,
  277.   fixed x0, fixed y0, fixed x3, fixed y3, fixed xt, fixed yt, floatp fraction)
  278. {    return gx_path_add_curve(ppath,
  279.             x0 + (fixed)((xt - x0) * fraction),
  280.             y0 + (fixed)((yt - y0) * fraction),
  281.             x3 + (fixed)((xt - x3) * fraction),
  282.             y3 + (fixed)((yt - y3) * fraction),
  283.             x3, y3);
  284. }
  285.  
  286. /* Append a path to another path, and reset the first path. */
  287. /* Currently this is only used to append a path to its parent */
  288. /* (the path in the previous graphics context). */
  289. int
  290. gx_path_add_path(gx_path *ppath, gx_path *ppfrom)
  291. {    subpath *psub;
  292.     path_unshare(ppfrom);
  293.     path_unshare(ppath);
  294.     if ( ppfrom->first_subpath )    /* i.e. ppfrom not empty */
  295.        {    if ( ppath->first_subpath )    /* i.e. ppath not empty */
  296.            {    subpath *psub = ppath->current_subpath;
  297.             segment *pseg = psub->last;
  298.             subpath *pfsub = ppfrom->first_subpath;
  299.             pseg->next = (segment *)pfsub;
  300.             pfsub->prev = pseg;
  301.            }
  302.         else
  303.             ppath->first_subpath = ppfrom->first_subpath;
  304.         ppath->current_subpath = ppfrom->current_subpath;
  305.         ppath->subpath_count += ppfrom->subpath_count;
  306.         ppath->curve_count += ppfrom->curve_count;
  307.        }
  308.     /* Transfer the remaining state. */
  309.     ppath->position = ppfrom->position;
  310.     ppath->position_valid = ppfrom->position_valid;
  311.     ppath->subpath_open = ppfrom->subpath_open;
  312.     gx_path_reset(ppfrom);        /* reset the source path */
  313.     return 0;
  314. }
  315.  
  316. /* Close the current subpath. */
  317. int
  318. gx_path_close_subpath(gx_path *ppath)
  319. {    subpath *psub = ppath->current_subpath;
  320.     register line_close_segment *lp;
  321.     if ( !ppath->subpath_open ) return 0;
  322.     path_alloc_segment(lp, line_close_segment, s_line_close,
  323.                "gx_path_close_subpath");
  324.     path_alloc_link(lp);
  325.     path_set_point(lp, psub->pt.x, psub->pt.y);
  326.     lp->sub = psub;
  327.     psub->closed = 1;
  328.     ppath->subpath_open = 0;
  329. #ifdef DEBUG
  330. if ( gs_debug['p'] )
  331.     if ( lp != 0 )
  332.       dprintf("[p]"), gx_print_segment((segment *)lp);
  333. #endif
  334.     return 0;
  335. }
  336.  
  337. /* ------ Internal routines ------ */
  338.  
  339. /* Copy the current path, because it was shared. */
  340. /* Return a pointer to the current subpath, or 0. */
  341. private subpath *
  342. path_alloc_copy(gx_path *ppath)
  343. {    gx_path path_new;
  344.     int code;
  345.     code = gx_path_copy(ppath, &path_new, 1);
  346.     if ( code < 0 ) return 0;
  347.     *ppath = path_new;
  348.     ppath->shares_segments = 0;
  349.     return ppath->current_subpath;
  350. }
  351.  
  352. /* ------ Debugging printout ------ */
  353.  
  354. #ifdef DEBUG
  355.  
  356. /* Forward references */
  357. void gx_path_print(P1(const gx_path *));
  358.  
  359. /* Print out a path with a label */
  360. void
  361. gx_dump_path(const gx_path *ppath, const char *tag)
  362. {    dprintf2("[p]Path %lx %s:\n", (ulong)ppath, tag);
  363.     gx_path_print(ppath);
  364. }
  365.  
  366. /* Print a path */
  367. void
  368. gx_path_print(const gx_path *ppath)
  369. {    const segment *pseg = (const segment *)ppath->first_subpath;
  370.     dprintf4("   subpaths=%d, curves=%d, point=(%f,%f)\n",
  371.          ppath->subpath_count, ppath->curve_count,
  372.          fixed2float(ppath->position.x),
  373.          fixed2float(ppath->position.y));
  374.     dprintf5("   box=(%f,%f),(%f,%f) last=%lx\n",
  375.          fixed2float(ppath->bbox.p.x), fixed2float(ppath->bbox.p.y),
  376.          fixed2float(ppath->bbox.q.x), fixed2float(ppath->bbox.q.y),
  377.          (ulong)ppath->box_last);
  378.     while ( pseg )
  379.        {    gx_print_segment(pseg);
  380.         pseg = pseg->next;
  381.        }
  382. }
  383. void
  384. gx_print_segment(const segment *pseg)
  385. {    char out[80];
  386.     sprintf(out, "   %lx<%lx,%lx>: %%s (%6g,%6g) ",
  387.         (ulong)pseg, (ulong)pseg->prev, (ulong)pseg->next,
  388.         fixed2float(pseg->pt.x), fixed2float(pseg->pt.y));
  389.     switch ( pseg->type )
  390.        {
  391.     case s_start:
  392. #define psub ((const subpath *)pseg)
  393.         dprintf1(out, "start");
  394.         dprintf2("#curves=%d last=%lx",
  395.              psub->curve_count, (ulong)psub->last);
  396. #undef psub
  397.         break;
  398.     case s_curve:
  399.         dprintf1(out, "curve");
  400. #define pcur ((const curve_segment *)pseg)
  401.         dprintf4("\n\tp1=(%f,%f) p2=(%f,%f)",
  402.              fixed2float(pcur->p1.x), fixed2float(pcur->p1.y),
  403.              fixed2float(pcur->p2.x), fixed2float(pcur->p2.y));
  404. #undef pcur
  405.         break;
  406.     case s_line:
  407.         dprintf1(out, "line");
  408.         break;
  409.     case s_line_close:
  410. #define plc ((const line_close_segment *)pseg)
  411.         dprintf1(out, "close");
  412.         dprintf1(" %lx", (ulong)(plc->sub));
  413. #undef plc
  414.         break;
  415.     default:
  416.        {    char t[20];
  417.         sprintf(t, "type 0x%x", pseg->type);
  418.         dprintf1(out, t);
  419.        }
  420.        }
  421.     dputc('\n');
  422. }
  423.  
  424. /* Print a clipping path */
  425. void
  426. gx_cpath_print(const gx_clip_path *pcpath)
  427. {    if ( pcpath->segments_valid )
  428.         gx_path_print(&pcpath->path);
  429.     else
  430.         dputs("   (segments not valid)\n");
  431.     dprintf5("   cbox=(%f,%f),(%f,%f) count=%d\n",
  432.          fixed2float(pcpath->cbox.p.x), fixed2float(pcpath->cbox.p.y),
  433.          fixed2float(pcpath->cbox.q.x), fixed2float(pcpath->cbox.q.y),
  434.          pcpath->list.count);
  435.     /****** SHOULD PRINT CLIP LIST ******/
  436. }
  437.  
  438. #endif                    /* DEBUG */
  439.